//
//  RelativeNewsTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/3.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "RelativeNewsTableViewCell.h"
#import "NetEaseImgView.h"
#import "UIImageView+WebCache.h"

#define relativeCellHeight 80

@implementation RelativeNewsTableViewCell {
    NetEaseImgView* imgView;       //左边的图片
    UILabel* titleLabel;        //新闻标题
    UILabel* sourceTimeLabel;   //来源和时间
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        
        imgView = [[NetEaseImgView alloc] init];
        [self.contentView addSubview:imgView];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.numberOfLines = 0;
        titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:titleLabel];
        
        sourceTimeLabel = [[UILabel alloc] init];
        sourceTimeLabel.textColor = [UIColor grayColor];
        sourceTimeLabel.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:sourceTimeLabel];
    }
    
    return self;
}

- (void)setRelativeNewsDic:(NSDictionary *)relativeNewsDic {
    if (_relativeNewsDic != relativeNewsDic) {
        _relativeNewsDic = [relativeNewsDic copy];
        
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //图片
    [imgView setFrame:CGRectMake(20, 10, 80, relativeCellHeight-20)];
    NSString* imgUrl = _relativeNewsDic[@"imgsrc"];
    if (imgUrl.length) {
        [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    } else [imgView setBgImg];
    
    //标题
    [titleLabel setFrame:CGRectMake(120, 10, kWidth-140, relativeCellHeight/2)];
    titleLabel.text = _relativeNewsDic[@"title"];
    
    //来源与时间
    NSString* source = _relativeNewsDic[@"source"];
    NSString* time = [_relativeNewsDic[@"ptime"] substringToIndex:10];
    [sourceTimeLabel setFrame:CGRectMake(120, relativeCellHeight-10-15, kWidth-150, 15)];
    sourceTimeLabel.text = [NSString stringWithFormat:@"%@  %@", source, time];
}

@end
